package Main;

    //<editor-fold defaultstate="collapsed" desc="Manager that handles all game buttons">

import java.io.IOException;
import javax.microedition.io.file.FileConnection;
import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.AlertType;
import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;

public class Button_Manager
   {
        public static final int inc_player_score        =   1;
        public static final int inc_opp_score           =   2;
        public static final int fire_weapon             =   3;

        public static final int single_player           =   4;
        public static final int multi_server            =   5;
        public static final int multi_client            =   6;


       private Game_Button buttons[]; //press, press2;
       private int[][] buttonGrid;
       private int MAX_BUTTONS = 3;
       
       Font f = Font.getFont(Font.FACE_PROPORTIONAL, Font.STYLE_PLAIN, Font.SIZE_LARGE);
       
       public Button_Manager(int width, int height, int max)
       {
           MAX_BUTTONS = max;
           buttons = new Game_Button[max + 1];
           buttonGrid = new int[width][height];
       }
       
       //<editor-fold defaultstate="collapsed" desc="Set a button's ID to know how to respond to a button press">
       void setButton(int buttonIndex, int buttonID)
       {
//           Main.PowerPongMIDlet.setMenuTitle("ID: " + buttonID);
           if(buttons[buttonIndex] != null)
               buttons[buttonIndex].ID = buttonID;
       }
       //</editor-fold>
       
       Game_Button getButton(int num)
       {
           return buttons[num];
       }
       
       /*  Button id must be larger than 0 and smaller than max_button, vertical decides how to draw buttons */
       void addButtons(String[] name, String[] name_inv, double x, double y, boolean vertical, int scaledWidth)
       {
           int err = 0;
               
           Image img1 = null;
           try {
               FileConnection fConn;
                   
               for(int id = 1; id <= MAX_BUTTONS; id++)
               {
                   try
                   {
                       img1 = Image.createImage(name[id-1]);
                   }
                   catch(Exception n)
                   {
                       Main.PowerPongMIDlet.menu.setTitle("Null detected");
                   }
               
                   err = 0;
                   // x and y here refers to actual pixel position
                   buttons[id] = new Game_Button(id);
                   err++;
                   // add image to created button
                   try
                   {
                       buttons[id].setImage(img1, Image.createImage(name_inv[id-1]), scaledWidth);
                   }
                   catch(IOException n)
                   {
//                       int scaledWidth = (int) (Main.PowerPongMIDlet.GetWidth() / 6);
                       buttons[id].setSize(scaledWidth, scaledWidth);
//                       setTitle("Null detected");
                   }
                   err++;
                   
                   if(vertical)
                        buttons[id].setLocation((int) (x * Main.PowerPongMIDlet.GetWidth() ), (int) (y * Main.PowerPongMIDlet.GetHeight() + (id-2) * (5+buttons[id].getHeight())));
                   else
                        buttons[id].setLocation((int) (x * Main.PowerPongMIDlet.GetWidth() + (id-1) * (buttons[id].getWidth())), (int) (y * Main.PowerPongMIDlet.GetHeight()));

                   err++;
                   
                   int width = buttons[id].width;
                   int height = buttons[id].height;
                   int thisx = buttons[id].x;
                   int thisy = buttons[id].y;
                   int ID = buttons[id].ID;
                   err = width+height+thisx+thisy+ID;
                   
                   try
                   {
                       // add new button to button grid
                       for (int a = 0; a < width; a++)
                       {
                           for (int b = 0; b < height; b++)
                           {
                               buttonGrid[(int)(thisx - width / 2 + a)][(int)(thisy - height/2 + b)] = id;
                           }
                       }
                   }
                   catch(Exception e)
                   {
                       Main.PowerPongMIDlet.menu.setTitle(width + " " + height + " " + thisx + " " + thisy + " " + ID);
                   }
               }
           } catch (Exception ex) {
               Main.PowerPongMIDlet.menu.setTitle("Error: " + err + " " +  ex.getMessage());
           }
       }
       
       /* Draw all the buttons in the array */
       public void DrawButtons(Graphics g) throws Exception
       {
//           int count = 0;
           for (int x = 1; x <= MAX_BUTTONS; x++)
           {
               if(buttons[x] == null) return;
//               count++;
               buttons[x].draw(g);
           }
       }
       
       //<editor-fold defaultstate="collapsed" desc="Respond to a button pressed on the game screen">
       public void ButtonPress(int x, int y)
       {
           int id = buttonGrid[x][y];
           
           boolean change = false;
           
           int funcID = 0;
           if(id>0)
               if(buttons[id] != null)
                    funcID = buttons[id].ID;
            
           if(funcID > 0)
           {
               switch(funcID)
               {
                   case fire_weapon:
                       paddle player = Main.PowerPongMIDlet.gfx.GetPlayer();
                       if(player.getAmmo() != 0 && Main.PowerPongMIDlet.gfx.reloadTime == 0)
                       {
                           Main.PowerPongMIDlet.gfx.reloadTime = Main.PowerPongMIDlet.fps/3;
                           Main.PowerPongMIDlet.gfx.forcedGenerate(1, false, player.getY() + player.getSize()/2); //type is bullet, isLeft is false, y-pos
                           if(Main.PowerPongMIDlet.getMode() != Main.PowerPongMIDlet.SINGLE)
                           {
                               Main.PowerPongMIDlet.bt.SendString("W " + "1" + "1" + Double.toString(player.getY() + player.getSize()/2));
                           }
                           player.decAmmo();
                           change = true;
                       }
                       break;
                   case single_player:
//                       change = true;
                       Main.PowerPongMIDlet.setMode(Main.PowerPongMIDlet.SINGLE);
                       Main.PowerPongMIDlet.createSingle();
                       break;
                   case multi_server:
//                       change = true;
                       Main.PowerPongMIDlet.setMode(Main.PowerPongMIDlet.SERVER);
                       Main.PowerPongMIDlet.createServer();
                       Main.PowerPongMIDlet.showAlert("Multiplayer status", "Created server", Alert.FOREVER);
                       break;
                   case multi_client:
//                       change = true;
                       Main.PowerPongMIDlet.setMode(Main.PowerPongMIDlet.CLIENT);
                       Main.PowerPongMIDlet.createClient();
//                       Main.PowerPongMIDlet.showAlert("Multiplayer status", "Finding server", 1000);
                       break;
                   default:
                       break;
               }
               
               if(buttons[id] != null && change)
               {
                   buttons[id].useImage(true);
                   Main.PowerPongMIDlet.menu.repaint();
               }
           }
       }
      
       //</editor-fold>
       //<editor-fold defaultstate="collapsed" desc="Respond to a button released on the game screen">
       public void ButtonRelease(int x, int y)
       {
           int id = buttonGrid[x][y];
           if(id > 0)
           {
               if(buttons[id] != null)
               {
                    buttons[id].useImage(false);
                    Main.PowerPongMIDlet.menu.repaint();
               }
           }
       }
       //</editor-fold>
       
       //<editor-fold defaultstate="collapsed" desc="a Game_button object that represents one button">
       private class Game_Button
       {
           int x, y;
           int height, width; // ratio of where on the screen the button's centre will appear
           
           Image image, imageINV, image_used;
           
           int ID;
           
           public Game_Button(int ID)
           {
               this.x = 0;
               this.y = 0;
               
               this.ID = ID;
               this.image = null;
               this.imageINV = null;
           }
           
           void setLocation(int x, int y)
           {
               this.x = x;
               this.y = y;
           }
           
           int getX() { return this.x; }
           int getY() { return this.y; }
           
           void setSize (int width, int height)
           {
               this.width = width;
               this.height = height;
           }
           
           int getWidth() { return this.width; }
           int getHeight() { return this.height; }
           
           int getID() { return ID; }
           
           Image scaledImage;
           Image scaledImageINV;
           //<editor-fold defaultstate="collapsed" desc="Set button's image to be displayed.">
           void setImage(Image image, Image imageINV, int scaledWidth)
           {
               // if scaled image isn't available create it
               if(scaledImage == null)
               {
                   scaledImage = Image.createImage(scaledWidth, (int)((double)(scaledWidth) / image.getWidth() * image.getHeight()));
                   scaledImageINV = Image.createImage(scaledWidth, (int)((double)(scaledWidth) / image.getWidth() * image.getHeight()));
                   
                   Graphics g = scaledImage.getGraphics();
                   Graphics gINV = scaledImageINV.getGraphics();
                   
                   for (int q = 0; q < scaledImage.getHeight(); q++)
                   {
                       for(int w = 0; w < scaledImage.getWidth(); w++)
                       {
                           g.setClip(w, q, 1, 1);
                           int dx = w * image.getWidth() / scaledImage.getWidth();
                           int dy = q * image.getHeight() / scaledImage.getHeight();
                           g.drawImage(image, w - dx, q - dy, Graphics.LEFT | Graphics.TOP);
                           
                           gINV.setClip(w, q, 1, 1);
                           gINV.drawImage(imageINV, w - dx, q - dy, Graphics.LEFT | Graphics.TOP);
                       }
                   }
               }
               
               this.image = scaledImage;
               this.imageINV = scaledImageINV;
               
               this.setSize(scaledImage.getWidth(), scaledImage.getHeight());
               
               this.image_used = this.image;
           }
           //</editor-fold>
           
           void draw(Graphics g) throws Exception
           {
               if(this.image_used == null)
               {
                   g.setColor(200,200,200);
                   g.fillRect(this.x - this.width/2, this.y - this.height/2, this.width, this.height);
               }
               else
                   g.drawImage(this.image_used, (int)(this.x - this.width/2), (int)(this.y - this.height/2), Graphics.LEFT | Graphics.TOP);           }
           
           //<editor-fold defaultstate="collapsed" desc="Set which image to use for drawing.">
           private void useImage(boolean INV)
           {
               if(INV)
                   this.image_used = this.imageINV;
               else
                   this.image_used = this.image;
           }
           //</editor-fold>
       }
       //</editor-fold>
   }
   //</editor-fold>
